package com.study.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.study.model.dto.CreateMemoDto;
import com.study.model.dto.CreateMemoRespDto;
import com.study.model.dto.UpdateMemoDto;
import com.study.model.dto.response.DailyMemoCountRespDto;
import com.study.model.entity.Memo;
import com.study.service.MemoService;
import com.study.util.QiniuUtil;
import com.study.util.result.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/memo")
public class MemoController {

    @Autowired
    private MemoService memoService;

    @Autowired
    private QiniuUtil qiniuUtil;

    @PostMapping
    public ApiResult createMemo(@RequestBody CreateMemoDto createMemoDto) {
        createMemoDto.setUserId(StpUtil.getLoginIdAsString());
        CreateMemoRespDto createMemoRespDto = memoService.createNewMemo(createMemoDto);
        return ApiResult.SUCCESS(createMemoRespDto);
    }

    @PostMapping("/taglist")
    public ApiResult showTagsList() {
        String userId = StpUtil.getLoginIdAsString();
        List<String> tagNames = memoService.findUserTagNames(userId);
        return ApiResult.SUCCESS(tagNames);
    }

    @PostMapping("/listmemo")
    public ApiResult listMemo(String queryTag) {
        String userId = StpUtil.getLoginIdAsString();
        List<Memo> memoList = memoService.findAllMemo(userId, queryTag);
        return ApiResult.SUCCESS(memoList);
    }

    @PostMapping("/deletememo")
    public ApiResult deleteMemo(String memeoId) {
        String userId = StpUtil.getLoginIdAsString();
        memoService.deleteMemoById(userId, memeoId);
        return ApiResult.SUCCESS();
    }

    @PostMapping("/updatememo")
    public ApiResult updateMemo(@RequestBody UpdateMemoDto updateMemoDto) {
        String userId = StpUtil.getLoginIdAsString();
        updateMemoDto.setUserId(userId);
        Memo memo = memoService.updateMemo(updateMemoDto);
        return ApiResult.SUCCESS(memo);
    }

    @PostMapping("/dailycount")
    public ApiResult dailyCount() {
        String userId = StpUtil.getLoginIdAsString();
        DailyMemoCountRespDto dailyMemoCountRespDto = memoService.dailyMemoCount(userId);
        return ApiResult.SUCCESS(dailyMemoCountRespDto);
    }

    @PostMapping("/file/token")
    public ApiResult uploadFile(){
        String token = qiniuUtil.getUploadToken();
        return ApiResult.SUCCESS(token);
    }
}
